//var gnLastEventId = 0;
var DT_ERROR = gnLastEventId++;
var DT_WARNING = gnLastEventId++;
var DT_NOTICE = gnLastEventId++;
var DT_VIEW_REQUESTED = gnLastEventId++;
var DT_VIEW_READY = gnLastEventId++;
var DT_EDIT_REQUESTED = gnLastEventId++;
var DT_EDIT_READY = gnLastEventId++;
var DT_ADD_REQUESTED = gnLastEventId++;
var DT_ADD_READY = gnLastEventId++;
var DT_UPDATE_REQUESTED = gnLastEventId++;
var DT_UPDATE_COMPLETE = gnLastEventId++;
var DT_INSERT_REQUESTED = gnLastEventId++;
var DT_INSERT_COMPLETE = gnLastEventId++;

function DataForm(FormID, XMLDefinitionFile, ObjectName)
{
	this.FormID = FormID;
	this.XMLDefinitionFile = XMLDefinitionFile;
	this.Criteria = new Array();
	this.ObjectName = ObjectName;
	this.settings = {
		FormID: FormID,
		FormDef: XMLDefinitionFile,
		ObjectName: ObjectName 
	};
	//event manager
    this.eventManager = new _eventManager();
	this.params = '';
	this.ServerPath = "/QuickerData/FormServer.php";
	this.PrimaryKey = null;
	
	/* register the known events */
    for (var i = 0; i < gnLastEventId; i++){
        this.registerEventID( i );
    }
}

/*
* Property Get/Sets
*/
DataForm.prototype.deleteRecord = function()
{
	/*
	$out .= "<script>function confirmDelete(){ if (confirm(\"Are you sure you want to delete this record?  This operation cannot be undone.\")) window.location.href=\"" . $_SERVER['SCRIPT_NAME'] . "?Delete=" . $this->PrimaryKeyValue . "\";}</script>";.
	*/
	me = this;
	$.get(this.ServerPath + "?" + this.params, $.extend(this.settings, {Action: "Delete"}),
	function(html)
	{
//		alert(html);
		me.RecordCount = Math.floor(html-0);
	});
}

// for Searching
DataForm.prototype.setFormElement = function(element)
{
	this.FormElement = element;
}

/*
* form section
*/

DataForm.prototype.SubmitSearchForm = function(args, message)
{
	var thing = $("input", this.SearchFormElement).serialize();
	thing = thing + $("textarea", this.SearchFormElement).serialize();
	alert(thing);
	this.ShowDataForm(thing, 'Apply filter...');

	//alert(thing);
}


/*
* Ajax functions
*/
DataForm.prototype.ShowDataForm = function(args, message)
{
	var me = this;
	me.triggerEvent(DT_SEARCH_FORM_REQUESTED, this.SearchFormElement);
	message = (message) ? message : 'Loading...';
	settings = (args) ? $.extend(this.settings, args) : this.settings;
	this.SearchFormElement.html(message).load(this.ServerPath + "?" + this.params, $.extend(settings, {Action: "getSearchControlHTML"}),
	function()
	{
		me.triggerEvent(DT_SEARCH_FORM_READY, me.SearchFormElement);
	});	
}

/*
* Event Manager, stolen from Kamap... thanks Kamap!
*/
DataForm.prototype.registerEventID = function( eventID ) {
    return this.eventManager.registerEventID(eventID);
};

DataForm.prototype.registerForEvent = function( eventID, obj, func ) {
    return this.eventManager.registerForEvent(eventID, obj, func);
};

DataForm.prototype.deregisterForEvent = function( eventID, obj, func ) {
    return this.eventManager.deregisterForEvent(eventID, obj, func);
};

DataForm.prototype.triggerEvent = function( eventID /*pass additional arguments*/ ) {
    return this.eventManager.triggerEvent.apply( this.eventManager, arguments );
};



//isObject(a)
//This function returns true if a is an object, array, or function. It returns false if a is a string, number, Boolean, null, or undefined.
function isObject(a)
{
	return (typeof a == 'object' && !!a) || isFunction(a);
};

//isString(a)
// This function returns true if a is a string. (but not a string object)
function isString(a)
{
	return typeof a == 'string';
};
function isFunction(a)
 {
      return typeof a == 'function';
 };